package com.zjdiepu.www.manage.api.icbc;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.BusiStatus;
import com.zjdiepu.www.base.enums.status.InterviewStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.base.enums.type.TypeData;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessVoMapper;
import com.zjdiepu.www.ibiz.other.IOtherInterviewConfigService;
import com.zjdiepu.www.ibiz.system.IBaseBusiUnitService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.other.dto.OtherInterviewConfig;
import com.zjdiepu.www.model.other.ext.OtherInterviewConfigExt;
import com.zjdiepu.www.model.system.dto.BaseBusiUnit;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.request.YwBusinessQueryRequest;

@RestController
@RequestMapping(value= "YwFaceToFaceSignController", produces="application/json;charset=UTF-8")
@Api(tags = {"yw_面见面签"}, produces="application/json;charset=UTF-8")
public class YwFaceToFaceSignController extends BaseController{

	@Resource
	private IYwBusinessService ywBusinessService;
	
	@Resource
    private YwBusinessVoMapper ywBusinessVoMapper;
	
	 @Resource
	    private IBaseBusiUnitService baseBusiUnitService;
	 @Resource
	 private IOtherInterviewConfigService otherInterViewConfigService;
	 
	 @ApiOperation(value = "面签成功",response = YwBusiness.class, httpMethod="POST", tags={"多表查询"})
	    @RequestMapping(value = "faceToFaceSuccess", method = RequestMethod.POST)
	 public ApiResponse faceToFaceSuccess(@ModelAttribute YwBusiness ywBusiness){
		 AbstractSysUser currentUser = getCurrentUser();
			YwBusiness ywBusiness2=ywBusinessService.selectById(ywBusiness.getId());
			if(ywBusiness2==null){
				return ApiResponse.fail(ResultFlagType.MISSING);
			}
			ywBusiness2.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
			ywBusiness2.setModifierName(currentUser.getName());
			ywBusiness2.setInterviewStatus(InterviewStatus.MIAN_QIAN_CHENG_GONG.getCode());
			ywBusinessService.saveUpdateAndDelete(ywBusiness2, OperateType.edit);
		 return ApiResponse.success(true);
	 }
	
	@ApiOperation(value = "修改面签状态",response = YwBusiness.class, httpMethod="POST", tags={"多表查询"})
    @RequestMapping(value = "updateInterviewStatus", method = RequestMethod.POST)
	public ApiResponse updateInterviewStatus(@Validated @ModelAttribute YwBusiness ywBusiness){
		 AbstractSysUser currentUser = getCurrentUser();
		YwBusiness ywBusiness2=ywBusinessService.selectById(ywBusiness.getId());
		if(ywBusiness2==null){
			return ApiResponse.fail(ResultFlagType.MISSING);
		}
		ywBusiness2.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
		ywBusiness2.setModifierName(currentUser.getName());
		ywBusiness2.setInterviewStatus(InterviewStatus.XU_YAO_ER_CI_MIAN_QIAN.getCode());
		ywBusinessService.saveUpdateAndDelete(ywBusiness2, OperateType.edit);
		return ApiResponse.success(true);
	}
	
	@ApiOperation(value = "条件查询业务信息",response = YwBusiness.class, httpMethod="POST", tags={"多表查询"})
    @RequestMapping(value = "multiList", method = RequestMethod.POST)
    public ApiResponse multiList(@Validated @ModelAttribute YwBusinessQueryRequest ywBusinessQueryRequest){
    	SysUser currentUser = (SysUser)getCurrentUser();
		PageVo page = getPage(ywBusinessQueryRequest);
		ywBusinessQueryRequest.setBaseWhere();
		ywBusinessQueryRequest.setBankLoan(currentUser.getLoanBank());
		ywBusinessQueryRequest.setTypeData(TypeData.SYSTEM.getCode());
		ywBusinessQueryRequest.setBusiStatusLess(BusiStatus.CHU_SHEN_DAN_CHU_SHEN_ZHONG.getCode());
		ywBusinessQueryRequest.setBusiStatusMore(BusiStatus.YIN_HANG_YI_FANG_KUAN.getCode());
        ywBusinessQueryRequest.setCodeUnits(currentUser.getCodeUnits());
        ywBusinessQueryRequest.setNeedLimit(true);
        StringBuffer sbSql=new StringBuffer();
//        sbSql.append(" AND (B.INTERVIEW_STATUS IS NULL OR B.INTERVIEW_STATUS=10 OR B.INTERVIEW_STATUS=100) AND B.BANK_LOAN='ICBC' ");
        sbSql.append(" AND (B.INTERVIEW_STATUS = 100 OR B.INTERVIEW_STATUS=200 OR B.INTERVIEW_STATUS=9000) AND B.BANK_LOAN='ICBC' "); // 修改为已经面签客户,pan
        if(StringUtils.isNotEmpty(ywBusinessQueryRequest.getUnitCode())){
        	sbSql.append(" AND BO.ORG_CODE = "+ywBusinessQueryRequest.getUnitCode());
        }
        ywBusinessQueryRequest.setWhereSql(sbSql.toString());
        ywBusinessQueryRequest.setOrderSql(" B.SUBMIT_TIME DESC ");
        int records = ywBusinessService.countMultiByExt(ywBusinessQueryRequest);
        page.setRecords(records);
        if (records > 0) {
            List<YwBusiness> ywBusinesss = ywBusinessService.selectMultiByExt(ywBusinessQueryRequest);
            BaseBusiUnit baseBusiUnit=null;
            String orgCode="";
            for (YwBusiness ywBusiness : ywBusinesss) {
            	baseBusiUnit=new BaseBusiUnit();
            	if(ywBusiness.getOrgCode().length()>4){
            		orgCode=ywBusiness.getOrgCode().substring(0,4);
            		baseBusiUnit.setOrgCode(orgCode);
            		baseBusiUnit=baseBusiUnitService.selectRefined(baseBusiUnit);
            		if(baseBusiUnit!=null){
            			ywBusiness.setFgsName(baseBusiUnit.getOrgName());
            		}
            	}
			}
            ywBusinessVoMapper.mapper(ywBusinesss);
            page.setRows(ywBusinesss);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
	}
	
	@ApiOperation(value = "二次面见面签列表",response = YwBusiness.class, httpMethod="POST", tags={"多表查询"})
    @RequestMapping(value = "multiTwoList", method = RequestMethod.POST)
    public ApiResponse multiTwoList(@Validated @ModelAttribute YwBusinessQueryRequest ywBusinessQueryRequest){
    	SysUser currentUser = (SysUser)getCurrentUser();
		PageVo page = getPage(ywBusinessQueryRequest);
		ywBusinessQueryRequest.setBaseWhere();
		ywBusinessQueryRequest.setBankLoan(currentUser.getLoanBank());
		// ywBusinessQueryRequest.setTypeData(TypeData.SYSTEM.getCode()); //不需要区分是人工还是接口，pan
		ywBusinessQueryRequest.setBusiStatusNqMore(BusiStatus.YIN_HANG_YI_FANG_KUAN.getCode());
        ywBusinessQueryRequest.setCodeUnits(currentUser.getCodeUnits());
        ywBusinessQueryRequest.setNeedLimit(true);
//        ywBusinessQueryRequest.setInterviewStatus(InterviewStatus.MIAN_QIAN_CHENG_GONG.getCode());
        StringBuffer sbSql=new StringBuffer();
        if(StringUtils.isNotEmpty(ywBusinessQueryRequest.getUnitCode())){
        	sbSql.append("  AND BO.ORG_CODE = "+ywBusinessQueryRequest.getUnitCode());
        }
//        sbSql.append(" AND `INTERVIEW_STATUS` =200 ");
        OtherInterviewConfigExt otherInterviewConfigExt=new OtherInterviewConfigExt();
    	otherInterviewConfigExt.setInterviewConfigStatus(InterviewStatus.WEI_MIAN_QIAN.getCode().toString());
    	List<OtherInterviewConfig> list=otherInterViewConfigService.selectByExt(otherInterviewConfigExt);
    	StringBuffer ssql=new StringBuffer();
    	if(list!=null&& list.size()>0){
    		ssql.append("  AND (");
    		for (OtherInterviewConfig otherInterviewConfig2 : list) {
    			if(otherInterviewConfig2.getBusiUnitCode()!=null){
//    				busiUnitCode+=otherInterviewConfig2.getBusiUnitCode()+",";
    				ssql.append(" B.SALESMAN_CODE LIKE ('"+otherInterviewConfig2.getBusiUnitCode()+"%') OR ");
    			}
			}
    		ssql=ssql.delete(ssql.length()-3,ssql.length());
    		ssql.append(")");
    	}
        sbSql.append(" AND (B.INTERVIEW_STATUS=10 OR B.INTERVIEW_STATUS IS NULL ) ");
        ywBusinessQueryRequest.setWhereSql(sbSql.toString()+ssql.toString());
        ywBusinessQueryRequest.setOrderSql(" B.INTERVIEW_TIME DESC ");
        int records = ywBusinessService.countMultiByExt(ywBusinessQueryRequest);
        page.setRecords(records);
        if (records > 0) {
            List<YwBusiness> ywBusinesss = ywBusinessService.selectMultiByExt(ywBusinessQueryRequest);
            BaseBusiUnit baseBusiUnit=null;
            String orgCode="";
            for (YwBusiness ywBusiness : ywBusinesss) {
            	baseBusiUnit=new BaseBusiUnit();
            	if(ywBusiness.getOrgCode().length()>4){
            		orgCode=ywBusiness.getOrgCode().substring(0,4);
            		baseBusiUnit.setOrgCode(orgCode);
            		baseBusiUnit=baseBusiUnitService.selectRefined(baseBusiUnit);
            		if(baseBusiUnit!=null){
            			ywBusiness.setFgsName(baseBusiUnit.getOrgName());
            		}
            	}
			}
            ywBusinessVoMapper.mapper(ywBusinesss);
            page.setRows(ywBusinesss);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
	}
}
